package persentation.presentDepartmentDean;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import po.userPO.StudentPO;
import po.userPO.UserPO;
import vo.userVO.StudentVO;
import businesslogic.chooseCourseBl.ChooseCourseController;
import businesslogic.userBl.StudentController;
import dataservice.userDataService.UserDatabaseService;



public class ShowStudentPanel extends JPanel implements ActionListener{
	private static final long serialVersionUID = 1L;
	private JTable table;
	private DepartmentdeanTabbedPane tabbedPane;
	private String depart;
	private ChooseCourseController cc;
	private Vector data,columnName;
	private MyModel model;	
	private ArrayList<StudentPO> studentPOs=new ArrayList<StudentPO>();
	private static final String ADDRESS_STRING="localhost";
	private JComboBox studentBox;
	private ArrayList<StudentVO> studentVOs;
	
	public ShowStudentPanel(ChooseCourseController c, String department) {
		super();
		setSize(900, 690);
		setLayout(null);
		this.depart = department;
		cc = c;

		columnName = new Vector();
		columnName.add("学生编号");
		columnName.add("姓名");
		columnName.add("年级");
		columnName.add("院系");
		columnName.add("总学分");
		columnName.add("总选修学分");
		columnName.add("此学期GPA");

		table = new JTable();
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		table.setFillsViewportHeight(true);
		JScrollPane jScrollPane = new JScrollPane(table);
		jScrollPane.setBounds(44, 64, 742, 351);
		add(jScrollPane);

		
		JButton button = new JButton("计算GPA和学分");
		button.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		button.setBounds(289, 446, 176, 23);
		add(button);
		
		studentBox = new JComboBox();
		studentBox.setBounds(72, 10, 113, 46);
		studentBox.setModel(new DefaultComboBoxModel(new String[] {"大一","大二","大三","大四"}));
		add(studentBox);
		studentBox.addActionListener(this);
		studentBox.setBorder(BorderFactory.createTitledBorder("年级"));
		
		findAllStudent();

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					cc.CalculateGPA(depart);
					JOptionPane.showMessageDialog(null, "计算成功","提示",JOptionPane.OK_OPTION);
					findAllStudent();
				} catch (Exception e2) {
					e2.printStackTrace();
					DepartmentdeanExceptionHandler.networkException();
				}
			}
		});
	}

	public void setTabbedPane(DepartmentdeanTabbedPane tabbedPane) {
		this.tabbedPane = tabbedPane;
	}
	
	public void findAllStudent() {
		try {
			int grade=studentBox.getSelectedIndex()+1;
			UserDatabaseService u = (UserDatabaseService) Naming
					.lookup("rmi://"+ADDRESS_STRING+":8888/userData");
			ArrayList<StudentPO> studentPOs=u.findStudentPOByTerm(grade, depart);
			data = new Vector();
			for (StudentPO c : studentPOs) {
				Vector v = new Vector();
				v.add(c.getId());
				v.add(c.getName());
				v.add(c.getGrade());
				v.add(c.getDepartmentId());
				v.add(c.getTotalCredit());
				v.add(c.getTotalSelectedCredit());
				if (c.getGPA(cc.NOW_TERM)==0){
					v.add("未计算");
				}else {
					v.add(c.getGPA(cc.NOW_TERM));
				}
				
				data.add(v);
			}
			model = new MyModel(data, columnName);
			table.setModel(model);
		} catch (RemoteException e) {
			DepartmentdeanExceptionHandler.networkException();
			System.out.println(e.getCause());
			System.out.println(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==studentBox){
			findAllStudent();
		}
	}
}
